package com.zzx.business.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzx.business.domain.BusinessSite;
import com.zzx.business.service.IBusinessSiteService;
import com.zzx.business.service.ProductService;
import com.zzx.common.annotation.Log;
import com.zzx.common.core.controller.BaseController;
import com.zzx.common.core.domain.AjaxResult;
import com.zzx.common.enums.BusinessType;
import com.zzx.common.utils.SecurityUtils;
import com.zzx.common.utils.poi.ExcelUtil;
import com.zzx.business.vo.BusinessFreightOrderVo;
import com.zzx.business.service.IBusinessFreightOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.zzx.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 运输信息Controller
 *
 * @author Z、zx
 * @date 2024-08-21
 */
@Api(tags = "运输信息Controller")
@RestController
@RequestMapping("/business/freightOrder")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class BusinessFreightOrderController extends BaseController {

    private final IBusinessFreightOrderService businessFreightOrderService;
    private final IBusinessSiteService businessSiteService;
    private final ProductService productService;

    @ApiOperation("查询运输信息列表")
    @PreAuthorize("@ss.hasPermi('business:freightOrder:list')")
    @GetMapping("/list")
    public TableDataInfo<BusinessFreightOrderVo> list(BusinessFreightOrderVo entity) {
        Long deptId = SecurityUtils.getDeptId();
        BusinessSite one = businessSiteService.getOne(Wrappers.<BusinessSite>lambdaQuery().eq(BusinessSite::getDeptId, deptId));
        if(one != null){
            String type = one.getType();
            if("1".equals(type)){
                entity.setStartSiteCode(one.getCode());
            } else if ("2".equals(type)) {
                entity.setEndSiteCode(one.getCode());
            }
        }
        return businessFreightOrderService.queryList(entity);
    }

    @ApiOperation("查询运输信息所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(BusinessFreightOrderVo entity) {
        return AjaxResult.success("查询成功", businessFreightOrderService.queryAll(entity));
    }

    @ApiOperation("导出运输信息列表")
    @PreAuthorize("@ss.hasPermi('business:freightOrder:export')")
    @Log(title = "运输信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusinessFreightOrderVo entity) {
        List<BusinessFreightOrderVo> list = businessFreightOrderService.queryAll(entity);
        ExcelUtil<BusinessFreightOrderVo> util = new ExcelUtil<>(BusinessFreightOrderVo.class);
        util.exportExcel(response, list, "运输信息数据");
    }

    @ApiOperation("获取运输信息详细信息")
    @PreAuthorize("@ss.hasPermi('business:freightOrder:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", businessFreightOrderService.queryById(id));
    }

    @ApiOperation("新增运输信息")
    @PreAuthorize("@ss.hasPermi('business:freightOrder:add')")
    @Log(title = "运输信息", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody BusinessFreightOrderVo entity) {
        return toAjax(productService.save(entity));
    }

    @ApiOperation("确认收货")
    @PreAuthorize("@ss.hasPermi('business:freightOrder:ispass')")
    @Log(title = "确认收货", businessType = BusinessType.UPDATE)
    @PostMapping("isPass")
    public AjaxResult isPass(@RequestBody BusinessFreightOrderVo entity) {
        return toAjax(productService.isPass(entity));
    }

    @ApiOperation("修改运输信息")
    @PreAuthorize("@ss.hasPermi('business:freightOrder:edit')")
    @Log(title = "运输信息", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody BusinessFreightOrderVo entity) {
        return toAjax(businessFreightOrderService.updateById(entity));
    }

    @ApiOperation("删除运输信息")
    @PreAuthorize("@ss.hasPermi('business:freightOrder:remove')")
    @Log(title = "运输信息", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(businessFreightOrderService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
